package com.mp.students.service.impl;

import com.mp.students.entity.MpUser;
import com.mp.students.dao.MpUserDao;
import com.mp.students.service.MPMpUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 *  里面参入了部分activiti服务代码
 * </p>
 *
 * @author mfc
 * @since 2018-09-14
 */
@Service
public class MpUserServiceImpl extends ServiceImpl<MpUserDao, MpUser> implements MPMpUserService {

    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private ProcessEngineConfigurationImpl processEngineConfiguration;

    @Override
    public void startProcess() {
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("employeeName", "Kermit");
        variables.put("numberOfDays", new Integer(4));
        variables.put("vacationMotivation", "I'm really tired!");
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("vacationRequest", variables);
        System.out.print("Number of process instances: " + runtimeService.createProcessInstanceQuery().count());
    }

    @Override
    public void checkProcess() {
        TaskQuery taskQuery = taskService.createTaskQuery();
        List<Task> tasks = taskQuery.list();
        for (Task task : tasks) {
            System.out.println("Task available: " + task.getName());
        }

        Task task = tasks.get(0);
        Map<String, Object> taskVariables = new HashMap<String, Object>();
        taskVariables.put("vacationApproved", "false");
        taskVariables.put("managerMotivation", "We have a tight deadline!");
        taskService.complete(task.getId(), taskVariables);

    }

}
